package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    //列表查询
    @GetMapping("/customers")
    public Result list(CustomerDto customerDto) {
        log.info("查询客户列表  " + customerDto);
        PageResult<Customer> pageResult = customerService.list(customerDto);
        return Result.success(pageResult);
    }

    //新增客户
    @PostMapping("/customers")
    public Result add(@RequestBody Customer customer) {
        log.info("新增客户  " + customer);
        customerService.add(customer);
        return Result.success();
    }

    //根据ID查询客户
    @GetMapping("/customers/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询客户  " + id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    //修改客户
    @PutMapping("/customers")
    public Result update(@RequestBody Customer customer) {
        log.info("修改客户  " + customer);
        customerService.update(customer);
        return Result.success();
    }
}
